using System;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.IO;
using TALoaders;
using TAAlert.Alerts;
using TAAlert.CommonUtils;

namespace TAAlert.GUI
{
    class MainWindow : Form
    {
        private TreeView alertTreeViewM;
        private MenuStrip menuStripM;
        private ToolStripMenuItem fileToolStripMenuItem;
        private ToolStripMenuItem exitToolStripMenuItem;
        private ToolStripMenuItem alertsToolStripMenuItem;
        private ToolStripMenuItem updateToolStripMenuItem;
        private ToolStripMenuItem helpToolStripMenuItem;
        private ToolStripMenuItem aboutToolStripMenuItem;
        private ToolStripMenuItem createNewToolStripMenuItem;
        private ToolStripMenuItem loadAlertsFromFileToolStripMenuItem;
        private ToolStripMenuItem saveAlertsToolStripMenuItem;
        private ToolStripMenuItem emaAlertToolStripMenuItemM;
        private ToolStripMenuItem twoEMAAlertToolStripMenuItemM;
        private ToolStripMenuItem removeToolStripMenuItemM;
        private ToolStripMenuItem backTestToolStripMenuItemM;
        private ToolStripMenuItem emaToolStripMenuItemM;
        private ToolStripMenuItem twoEMAToolStripMenuItemM;
        private ToolStripMenuItem mACDToolStripMenuItem;
        private ToolStripMenuItem saveLoaderCacheToolStripMenuItem;
        private ToolStripMenuItem saveToolStripMenuItem;
        private ToolStripMenuItem loadToolStripMenuItem;
        private ToolStripMenuItem mACDToolStripMenuItem1;
        private ToolStripMenuItem vEMAToolStripMenuItem;
        private ToolStripMenuItem analysisToolStripMenuItem;
        private ToolStripMenuItem vEMAToolStripMenuItem1;
        private ToolStripMenuItem twoEMAToolStripMenuItem;
        private ToolStripMenuItem mACDToolStripMenuItem2;
        private ToolStripMenuItem vEMAToolStripMenuItem2;
        private ToolStripMenuItem vEMAToolStripMenuItem3;

        AlertManager alertsM = new AlertManager();
        
        // Constructor
        public MainWindow()
        {
            InitializeComponent();
        }

        private void quit(object sender, EventArgs e)
        {
            this.Close();
        }

         // Auto generated by Designer
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainWindow));
            this.alertTreeViewM = new System.Windows.Forms.TreeView();
            this.menuStripM = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveLoaderCacheToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.alertsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.loadAlertsFromFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveAlertsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.updateToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.createNewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.emaAlertToolStripMenuItemM = new System.Windows.Forms.ToolStripMenuItem();
            this.twoEMAAlertToolStripMenuItemM = new System.Windows.Forms.ToolStripMenuItem();
            this.mACDToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.removeToolStripMenuItemM = new System.Windows.Forms.ToolStripMenuItem();
            this.backTestToolStripMenuItemM = new System.Windows.Forms.ToolStripMenuItem();
            this.emaToolStripMenuItemM = new System.Windows.Forms.ToolStripMenuItem();
            this.twoEMAToolStripMenuItemM = new System.Windows.Forms.ToolStripMenuItem();
            this.mACDToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.vEMAToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.analysisToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.vEMAToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.twoEMAToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mACDToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
            this.vEMAToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.vEMAToolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStripM.SuspendLayout();
            this.SuspendLayout();
            // 
            // alertTreeViewM
            // 
            this.alertTreeViewM.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.alertTreeViewM.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.alertTreeViewM.Location = new System.Drawing.Point(12, 27);
            this.alertTreeViewM.Name = "alertTreeViewM";
            this.alertTreeViewM.Size = new System.Drawing.Size(418, 327);
            this.alertTreeViewM.TabIndex = 6;
            // 
            // menuStripM
            // 
            this.menuStripM.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.alertsToolStripMenuItem,
            this.backTestToolStripMenuItemM,
            this.analysisToolStripMenuItem,
            this.helpToolStripMenuItem});
            this.menuStripM.Location = new System.Drawing.Point(0, 0);
            this.menuStripM.Name = "menuStripM";
            this.menuStripM.Size = new System.Drawing.Size(442, 24);
            this.menuStripM.TabIndex = 7;
            this.menuStripM.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.saveLoaderCacheToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
            this.fileToolStripMenuItem.Text = "File";
            // 
            // saveLoaderCacheToolStripMenuItem
            // 
            this.saveLoaderCacheToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.saveToolStripMenuItem,
            this.loadToolStripMenuItem});
            this.saveLoaderCacheToolStripMenuItem.Name = "saveLoaderCacheToolStripMenuItem";
            this.saveLoaderCacheToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
            this.saveLoaderCacheToolStripMenuItem.Text = "Loader Cache";
            // 
            // saveToolStripMenuItem
            // 
            this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
            this.saveToolStripMenuItem.Size = new System.Drawing.Size(111, 22);
            this.saveToolStripMenuItem.Text = "Save";
            this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveLoaderCache_Click);
            // 
            // loadToolStripMenuItem
            // 
            this.loadToolStripMenuItem.Name = "loadToolStripMenuItem";
            this.loadToolStripMenuItem.Size = new System.Drawing.Size(111, 22);
            this.loadToolStripMenuItem.Text = "Open";
            this.loadToolStripMenuItem.Click += new System.EventHandler(this.openLoaderCache_Click);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
            this.exitToolStripMenuItem.Text = "E&xit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.quit);
            // 
            // alertsToolStripMenuItem
            // 
            this.alertsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.loadAlertsFromFileToolStripMenuItem,
            this.saveAlertsToolStripMenuItem,
            this.updateToolStripMenuItem,
            this.createNewToolStripMenuItem,
            this.removeToolStripMenuItemM});
            this.alertsToolStripMenuItem.Name = "alertsToolStripMenuItem";
            this.alertsToolStripMenuItem.Size = new System.Drawing.Size(47, 20);
            this.alertsToolStripMenuItem.Text = "Alerts";
            // 
            // loadAlertsFromFileToolStripMenuItem
            // 
            this.loadAlertsFromFileToolStripMenuItem.Name = "loadAlertsFromFileToolStripMenuItem";
            this.loadAlertsFromFileToolStripMenuItem.Size = new System.Drawing.Size(168, 22);
            this.loadAlertsFromFileToolStripMenuItem.Text = "&Load Alerts";
            this.loadAlertsFromFileToolStripMenuItem.Click += new System.EventHandler(this.loadAlerts);
            // 
            // saveAlertsToolStripMenuItem
            // 
            this.saveAlertsToolStripMenuItem.Name = "saveAlertsToolStripMenuItem";
            this.saveAlertsToolStripMenuItem.Size = new System.Drawing.Size(168, 22);
            this.saveAlertsToolStripMenuItem.Text = "&Save Alerts";
            this.saveAlertsToolStripMenuItem.Click += new System.EventHandler(this.saveAlerts);
            // 
            // updateToolStripMenuItem
            // 
            this.updateToolStripMenuItem.Name = "updateToolStripMenuItem";
            this.updateToolStripMenuItem.Size = new System.Drawing.Size(168, 22);
            this.updateToolStripMenuItem.Text = "Update";
            this.updateToolStripMenuItem.Click += new System.EventHandler(this.updateAlerts);
            // 
            // createNewToolStripMenuItem
            // 
            this.createNewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.emaAlertToolStripMenuItemM,
            this.twoEMAAlertToolStripMenuItemM,
            this.mACDToolStripMenuItem1,
            this.vEMAToolStripMenuItem3});
            this.createNewToolStripMenuItem.Name = "createNewToolStripMenuItem";
            this.createNewToolStripMenuItem.Size = new System.Drawing.Size(168, 22);
            this.createNewToolStripMenuItem.Text = "Create New Alert";
            // 
            // emaAlertToolStripMenuItemM
            // 
            this.emaAlertToolStripMenuItemM.Name = "emaAlertToolStripMenuItemM";
            this.emaAlertToolStripMenuItemM.Size = new System.Drawing.Size(152, 22);
            this.emaAlertToolStripMenuItemM.Text = "EMA";
            this.emaAlertToolStripMenuItemM.Click += new System.EventHandler(this.CreateNewAlert_Click);
            // 
            // twoEMAAlertToolStripMenuItemM
            // 
            this.twoEMAAlertToolStripMenuItemM.Name = "twoEMAAlertToolStripMenuItemM";
            this.twoEMAAlertToolStripMenuItemM.Size = new System.Drawing.Size(152, 22);
            this.twoEMAAlertToolStripMenuItemM.Text = "TwoEMA";
            this.twoEMAAlertToolStripMenuItemM.Click += new System.EventHandler(this.CreateNewAlert_Click);
            // 
            // mACDToolStripMenuItem1
            // 
            this.mACDToolStripMenuItem1.Name = "mACDToolStripMenuItem1";
            this.mACDToolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
            this.mACDToolStripMenuItem1.Text = "MACD";
            this.mACDToolStripMenuItem1.Click += new System.EventHandler(this.CreateNewAlert_Click);
            // 
            // removeToolStripMenuItemM
            // 
            this.removeToolStripMenuItemM.Name = "removeToolStripMenuItemM";
            this.removeToolStripMenuItemM.Size = new System.Drawing.Size(168, 22);
            this.removeToolStripMenuItemM.Text = "Remove";
            this.removeToolStripMenuItemM.Click += new System.EventHandler(this.removeToolStripMenuItem_Click);
            // 
            // backTestToolStripMenuItemM
            // 
            this.backTestToolStripMenuItemM.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.emaToolStripMenuItemM,
            this.twoEMAToolStripMenuItemM,
            this.mACDToolStripMenuItem,
            this.vEMAToolStripMenuItem});
            this.backTestToolStripMenuItemM.Name = "backTestToolStripMenuItemM";
            this.backTestToolStripMenuItemM.Size = new System.Drawing.Size(62, 20);
            this.backTestToolStripMenuItemM.Text = "&BackTest";
            // 
            // emaToolStripMenuItemM
            // 
            this.emaToolStripMenuItemM.Name = "emaToolStripMenuItemM";
            this.emaToolStripMenuItemM.Size = new System.Drawing.Size(126, 22);
            this.emaToolStripMenuItemM.Text = "EMA";
            this.emaToolStripMenuItemM.Click += new System.EventHandler(this.BackTestToolStrip_Click);
            // 
            // twoEMAToolStripMenuItemM
            // 
            this.twoEMAToolStripMenuItemM.Name = "twoEMAToolStripMenuItemM";
            this.twoEMAToolStripMenuItemM.Size = new System.Drawing.Size(126, 22);
            this.twoEMAToolStripMenuItemM.Text = "TwoEMA";
            this.twoEMAToolStripMenuItemM.Click += new System.EventHandler(this.BackTestToolStrip_Click);
            // 
            // mACDToolStripMenuItem
            // 
            this.mACDToolStripMenuItem.Name = "mACDToolStripMenuItem";
            this.mACDToolStripMenuItem.Size = new System.Drawing.Size(126, 22);
            this.mACDToolStripMenuItem.Text = "MACD";
            this.mACDToolStripMenuItem.Click += new System.EventHandler(this.BackTestToolStrip_Click);
            // 
            // vEMAToolStripMenuItem
            // 
            this.vEMAToolStripMenuItem.Name = "vEMAToolStripMenuItem";
            this.vEMAToolStripMenuItem.Size = new System.Drawing.Size(126, 22);
            this.vEMAToolStripMenuItem.Text = "VEMA";
            this.vEMAToolStripMenuItem.Click += new System.EventHandler(this.BackTestToolStrip_Click);
            // 
            // analysisToolStripMenuItem
            // 
            this.analysisToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.vEMAToolStripMenuItem1,
            this.twoEMAToolStripMenuItem,
            this.mACDToolStripMenuItem2,
            this.vEMAToolStripMenuItem2});
            this.analysisToolStripMenuItem.Name = "analysisToolStripMenuItem";
            this.analysisToolStripMenuItem.Size = new System.Drawing.Size(57, 20);
            this.analysisToolStripMenuItem.Text = "Analyse";
            // 
            // vEMAToolStripMenuItem1
            // 
            this.vEMAToolStripMenuItem1.Name = "vEMAToolStripMenuItem1";
            this.vEMAToolStripMenuItem1.Size = new System.Drawing.Size(126, 22);
            this.vEMAToolStripMenuItem1.Text = "EMA";
            this.vEMAToolStripMenuItem1.Click += new System.EventHandler(this.analyseStrategy);
            // 
            // twoEMAToolStripMenuItem
            // 
            this.twoEMAToolStripMenuItem.Name = "twoEMAToolStripMenuItem";
            this.twoEMAToolStripMenuItem.Size = new System.Drawing.Size(126, 22);
            this.twoEMAToolStripMenuItem.Text = "TwoEMA";
            this.twoEMAToolStripMenuItem.Click += new System.EventHandler(this.analyseStrategy);
            // 
            // mACDToolStripMenuItem2
            // 
            this.mACDToolStripMenuItem2.Name = "mACDToolStripMenuItem2";
            this.mACDToolStripMenuItem2.Size = new System.Drawing.Size(126, 22);
            this.mACDToolStripMenuItem2.Text = "MACD";
            this.mACDToolStripMenuItem2.Click += new System.EventHandler(this.analyseStrategy);
            // 
            // vEMAToolStripMenuItem2
            // 
            this.vEMAToolStripMenuItem2.Name = "vEMAToolStripMenuItem2";
            this.vEMAToolStripMenuItem2.Size = new System.Drawing.Size(126, 22);
            this.vEMAToolStripMenuItem2.Text = "VEMA";
            this.vEMAToolStripMenuItem2.Click += new System.EventHandler(this.analyseStrategy);
            // 
            // helpToolStripMenuItem
            // 
            this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.aboutToolStripMenuItem});
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.Size = new System.Drawing.Size(40, 20);
            this.helpToolStripMenuItem.Text = "Help";
            // 
            // aboutToolStripMenuItem
            // 
            this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            this.aboutToolStripMenuItem.Size = new System.Drawing.Size(114, 22);
            this.aboutToolStripMenuItem.Text = "About";
            this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
            // 
            // vEMAToolStripMenuItem3
            // 
            this.vEMAToolStripMenuItem3.Name = "vEMAToolStripMenuItem3";
            this.vEMAToolStripMenuItem3.Size = new System.Drawing.Size(152, 22);
            this.vEMAToolStripMenuItem3.Text = "VEMA";
            this.vEMAToolStripMenuItem3.Click += new System.EventHandler(this.CreateNewAlert_Click);
            // 
            // MainWindow
            // 
            this.ClientSize = new System.Drawing.Size(442, 366);
            this.Controls.Add(this.alertTreeViewM);
            this.Controls.Add(this.menuStripM);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MainMenuStrip = this.menuStripM;
            this.Name = "MainWindow";
            this.Text = "Technical Analysis Alerts";
            this.menuStripM.ResumeLayout(false);
            this.menuStripM.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void loadAlerts(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Alert files (*.alrt;*.xml)|*.xml;*.alrt";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                alertsM.loadAlertsFromXML(dlg.FileName);
                this.updateAlerts(sender, e);
            }
        }

        private void updateAlerts(object sender, EventArgs e)
        {
            this.alertTreeViewM.Nodes.Clear();

            foreach (Alert a in alertsM.Alerts)
            {
                string[] info;
                Signal sNow = a.evalAlert(out info);
                Signal sOld = a.oldAlert();

                string rootNodeName = string.Format("Ticker:{0,-25} Signal:{1,4}", a.Ticker, sNow.ToString());
                TreeNode rootNode = this.alertTreeViewM.Nodes.Add(rootNodeName);
                if (sNow == Signal.Buy && sOld == Signal.Buy)
                    rootNode.BackColor = Color.LightGreen;
                else if (sNow == Signal.Buy && sOld == Signal.Sell)
                    rootNode.BackColor = Color.Green;
                else if (sNow == Signal.Sell && sOld == Signal.Sell)
                    rootNode.BackColor = Color.LightPink;
                else if (sNow == Signal.Sell && sOld == Signal.Buy)
                    rootNode.BackColor = Color.Red;
                else
                    rootNode.BackColor = Color.Aqua;

                rootNode.Nodes.Add(a.Description);
                foreach (string line in info)
                    rootNode.Nodes.Add(line);
            }
        }

        private void saveAlerts(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = "alrt";
            dlg.Filter = "Alert files (*.alrt;*.xml)|*.xml;*.alrt";
            if (dlg.ShowDialog() == DialogResult.OK)
                alertsM.saveAlertsToXML(dlg.FileName);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.AppendLine("Technical Analysis Alerts");
            sBuilder.AppendLine("(c) 2010 Marek Ozana");
            MessageBox.Show(sBuilder.ToString());
        }

        private void CreateNewAlert_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            StrategyName strategyName = (StrategyName)Enum.Parse(typeof(StrategyName), item.Text);

            CreateNewAlertGUI emaForm = new CreateNewAlertGUI(strategyName);
            
            emaForm.ShowDialog();
            if (emaForm.Alert != null)
            {
                alertsM.addAlert(emaForm.Alert);
                this.updateAlerts(sender, e);
            }
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveAlert removeForm = new RemoveAlert(alertsM);
            removeForm.ShowDialog();
            this.updateAlerts(sender, e);
        }

        private void BackTestToolStrip_Click(object sender, EventArgs e)
        {
            StrategyName strategy = (StrategyName) Enum.Parse(typeof(StrategyName), sender.ToString());
            GetBackTestParametersGUI getEMABackTestParams = new GetBackTestParametersGUI(strategy);
            getEMABackTestParams.ShowDialog();
        }

        private void saveLoaderCache_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = "cache";
            dlg.Filter = "Loader Cache files (*.cache)|*.cache";
            if (dlg.ShowDialog() == DialogResult.OK)
                CachedDownloader.saveCacheToFile(dlg.FileName);
        }

        private void openLoaderCache_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = "cache";
            dlg.Filter = "Loader Cache files (*.cache)|*.cache";
            if (dlg.ShowDialog() == DialogResult.OK)
                CachedDownloader.openCacheFromFile(dlg.FileName);
            
        }

        private void analyseStrategy(object sender, EventArgs e)
        {
            StrategyName strategy = (StrategyName)Enum.Parse(typeof(StrategyName), sender.ToString());
            StrategyDetailsGUI sDetail = new StrategyDetailsGUI(strategy);
            sDetail.Show();
        }
    }
}
